﻿@{
    ViewData["Title"] = "BinLocationUpd";
    Layout = "~/Views/Shared/Template.cshtml";
}

<form class="layui-form" action="/Warehouse/BinLocationUpd" method="post">
    <div class="layui-form-item layui-row">
        <div class="layui-col-md4 layui-col-sm6">
            <label class="layui-form-label">库位编号:</label>
            <div class="layui-input-block">
                <input type="text" name="binLocationNo" id="binLocationNo" lay-verify="required" placeholder="可不填写，自动生成" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-col-md4 layui-col-sm6">
            <label class="layui-form-label">仓库:</label>
            <div class="layui-input-block">
                <select name="warehouseId" id="warehouseId" lay-filter="aihao"></select>
            </div>
        </div>
    </div>

    <div class="layui-form-item layui-row">
        <div class="layui-col-md4 layui-col-sm6">
            <label class="layui-form-label">库位名称:</label>
            <div class="layui-input-block">
                <input type="text" name="binLocationName" id="binLocationName" required lay-verify="required" placeholder="请输入库位名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-col-md4 layui-col-sm6">
            <label class="layui-form-label">仓库类型:</label>
            <div class="layui-input-block">
                <select name="warehouseLocationId" id="warehouseLocationId" lay-filter="aihao"></select>
            </div>
        </div>
    </div>

    <div class="layui-form-item layui-row">
        <div class="layui-col-md4 layui-col-sm6">
            <label class="layui-form-label">是否默认:</label>
            <div class="layui-input-block">
                <input type="checkbox" name="isDefault" id="isDefault" lay-skin="primary">
            </div>
        </div>
        <div class="layui-col-md4 layui-col-sm6">
            <label class="layui-form-label">是否禁用:</label>
            <div class="layui-input-block">
                <input type="checkbox" name="isDisable" id="isDisable" lay-skin="primary">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">取消</button>
        </div>
    </div>
</form>

<script>
    //文档就绪函数
    $(function () {
        warehouses();
        warehouseLocations();
        fantian();
    });
    //仓库下拉框
    function warehouses() {
        layui.use('form', function () {
            var form = layui.form;
            $.ajax({
                url: "/Warehouse/GetWarehouseLShow",
                type: "get",
                data: { token: localStorage.getItem("token") },
                success: function (data) {
                    var data = JSON.parse(data)
                    console.log(data.data)
                    var options = '<option value="">请选择</option>'; // 添加默认选项
                    $.each(data.data, function (index, item) {
                        options += '<option value="' + item.warehouseId + '">' + item.warehouseName + '</option>';
                    });
                    $("#warehouseId").html(options);
                    form.render('select'); // 重新渲染下拉框
                },
                error: function () {
                    console.log("获取数据失败");
                    // 可以考虑显示一个错误消息给用户
                }
            });
        });
    }
    
    //库位下拉框
    function warehouseLocations(){
        layui.use('form', function () {
            var form = layui.form;
            $.ajax({
                url: "/Warehouse/GetWarehouseLocations",
                type: "get",
                data: { token: localStorage.getItem("token") },
                success: function (data) {
                    var data = JSON.parse(data)
                    //console.log(data.data)
                    var options = '<option value="">请选择</option>'; // 添加默认选项
                    $.each(data.data, function (index, item) {
                        options += '<option value="' + item.warehouseLocationId + '">' + item.warehouseLocationName + '</option>';
                    });
                    $("#warehouseLocationId").html(options);
                    form.render('select'); // 重新渲染下拉框
                },
                error: function () {
                    console.log("获取数据失败");
                    // 可以考虑显示一个错误消息给用户
                }
            });
        });
    }
    
    var obj = {
        binLocationId: 0,
        binLocationRemarks: '',
        office_id: '',
        user_id: '',
        createName: '',
        createTime: '',
        updateName: '',
        updateTime: '',
        deleteName: '',
        deleteTime: '',
        isDelete: ''
    };

    //反填
    function fantian() {
        return new Promise(function (resolve, reject) {
            $(document).ready(function () {
                // 获取要编辑的用户ID，可以通过URL参数或者其他方式获取，这里假设通过URL参数获取
                var binLocationId = localStorage.getItem("binLocationId");
                console.log(binLocationId);
                // 如果获取到了用户ID，就去后台获取该用户的详细信息并反填到表单
                if (binLocationId) {
                    $.ajax({
                        type: "get",
                        url: "/Warehouse/GetBinLocationFan",
                        data: {
                            binLocationId: binLocationId,
                            token: localStorage.getItem("token")
                        },
                        success: function (res) {
                            var res = JSON.parse(res)
                            if (res.data) {
                                const binLocationData = res.data;
                                // 将获取到的用户数据反填到表单相应字段
                                $('#binLocationNo').val(binLocationData.binLocationNo);
                                $('#binLocationName').val(binLocationData.binLocationName);
                                $('#isDisable').val(binLocationData.isDisable);
                                //反填多选框
                                $('#warehouseId').val(binLocationData.warehouseId);//反填下拉框
                                layui.form.render('select')
                                $('#warehouseLocationId').val(binLocationData.warehouseLocationId);
                                //是否默认
                                layui.form.render('select')
                                if (binLocationData.isDefault == true) {
                                    $('#isDefault').prop('checked', true)
                                } else {
                                    $('#isDefault').prop('checked', false)
                                }
                                //是否禁用
                                if (binLocationData.isDisable == true) {
                                    $('#isDisable').prop('checked', true)
                                } else {
                                    $('#isDisable').prop('checked', false)
                                }
                                

                                obj.binLocationId = binLocationData.binLocationId;
                                obj.binLocationRemarks = binLocationData.binLocationRemarks
                                obj.office_id = binLocationData.office_id
                                obj.user_id = binLocationData.user_id
                                obj.createName = binLocationData.createName;
                                obj.createTime = binLocationData.createTime;
                                obj.updateName = binLocationData.updateName;
                                obj.updateTime = binLocationData.updateTime;
                                obj.deleteName = binLocationData.deleteName;
                                obj.deleteTime = binLocationData.deleteTime;
                                obj.isDelete = binLocationData.isDelete;
                                console.log(res.data);
                                resolve(binLocationData);
                            } else {
                                layui.layer.msg(res.message, { icon: 2 });
                                reject(res.message);
                            }
                        },
                        error: function () {
                            layui.layer.msg('获取用户信息失败，请稍后重试', { icon: 2 });
                            reject('获取用户信息失败');
                        }
                    });
                } else {
                    reject('未获取到有效的仓库ID');
                }
            });
        });
    }
    //修改
    var obj1 = {
        binLocationId: 0,
        binLocationNo:'',
        binLocationName: '',
        isDefault: '',
        isDisable: '',
        warehouseId:'',
        warehouseLocationId: '',
        binLocationRemarks: '',
        office_id: '',
        user_id: '',
        createName: '',
        createTime: '',
        updateName: '',
        updateTime: '',
        deleteName: '',
        deleteTime: '',
        isDelete: ''
    };
    layui.use(['layer', 'form', 'util'], function () {
        var layer = layui.layer;
        var $ = layui.jquery;
        var form = layui.form;
        var util = layui.util;

        form.on('submit(formDemo)', function (data) {
            var field = data.field; // 获取表单字段值

            obj1.binLocationId = obj.binLocationId,
                obj1.binLocationNo = field.binLocationNo,
                obj1.binLocationName = field.binLocationName,
                obj1.isDefault = field.isDefault,
                obj1.isDisable = field.isDisable,
                obj1.warehouseId = field.warehouseId,
                obj1.warehouseLocationId = field.warehouseLocationId,
                obj1.binLocationRemarks = field.binLocationRemarks,
                obj1.office_id = obj.office_id,
                obj1.user_id = obj.user_id,
                obj1.user_id = obj.user_id,
                obj1.createName = obj.createName,
                obj1.createTime = obj.createTime,
                obj1.updateName = obj.updateName,
                obj1.updateTime = obj.updateTime,
                obj1.deleteName = obj.deleteName,
                obj1.deleteTime = obj.deleteTime,
                obj1.isDelete = obj.isDelete
            // 根据是否选中复选框设置isDefault的值
            obj1.isDefault = $('#isDefault').is(':checked') ? true : false;
            obj1.isDisable = $('#isDisable').is(':checked') ? true : false;

            // 显示填写结果，仅作演示用
            // 此处可执行 Ajax 等操作

            $.ajax({
                url: '/Warehouse/GetBinLocationUpd',
                type: 'post',
                data: {
                    obj1:obj1,
                    token: localStorage.getItem("token")
                },
                success: function (res) {
                    var res = JSON.parse(res)
                    console.log(obj1);
                    console.log(res);
                    if (res.code == 1) {
                        layer.msg("修改成功", { icon: 1 })
                         setTimeout(function () {
                        location.href = '/Warehouse/BinLocationShow'
                    }, 2000);
                    } else {
                        layer.msg("修改失败:" + res.message, { icon: 2 });
                    }
                }
            });

            return false; // 阻止默认 form 跳转
        });
        // 对弹层中的表单进行初始化渲染
        form.render();
    });
</script>


